<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { UserFilled, Lock } from '@element-plus/icons-vue'
import useUserStore from '@/store/modules/user'
import { ElNotification } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import { getDayRegions } from '@/utils/time'
const userStore = useUserStore()
const $route = useRoute()
const $router = useRouter()
// 收集表单中的手机号和密码
const formLogin = reactive({
  username: 'admin',
  password: 'atguigu123',
})
const selfValidateUsername = (rule: any, value: any, cb: any) => {
  if (!value.trim()) cb(new Error('请输入用户名'))
  cb()
}
const selfValidatePassword = (rule: any, value: any, cb: any) => {
  if (!value.trim()) cb(new Error('请输入密码'))
  cb()
}
// 设置form的规则
const rules = {
  username: [
    { validator: selfValidateUsername, trigger: 'blur' },
    { required: true, min: 4, max: 8, message: '长度4-8位', trigger: 'blur' },
  ],
  password: [
    { validator: selfValidatePassword, trigger: 'blur' },
    {
      required: true,
      min: 6,
      max: 12,
      message: '长度6-12位',
      trigger: 'change',
    },
  ],
}

// 设置form的ref
const loginForm = ref()
const isLoginStatus = ref(false)
const login = async () => {
  await loginForm.value.validate()
  isLoginStatus.value = true
  try {
    await userStore.userLogin(formLogin)
    await $router.push({ path: ($route.query.redirect as string) || '/' })
    ElNotification({
      title: `${getDayRegions()}`,
      message: '登录成功',
      type: 'success',
    })
  } catch (error) {
    ElNotification({
      message: (error as Error).message,
      type: 'error',
    })
  } finally {
    isLoginStatus.value = false
  }
}
</script>

<template>
  <div class="container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="formArea"
          :model="formLogin"
          :rules="rules"
          ref="loginForm"
        >
          <div class="title">Hello 靓仔</div>
          <div class="content">welcome to my project</div>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="UserFilled"
              v-model="formLogin.username"
              type="text"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="formLogin.password"
              :prefix-icon="Lock"
              type="password"
              show-password
            ></el-input>
          </el-form-item>
          <!-- 登录 -->
          <el-form-item>
            <el-button
              :loading="isLoginStatus"
              @click="login"
              type="primary"
              class="btn"
            >
              Login
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .formArea {
    position: relative;
    top: 40vh;
    left: 30px;
    width: 80%;
    padding: 40px;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    .title {
      font-size: #fff;
      font-size: 32px;
    }
    .content {
      padding: 10px 0;
    }
    .btn {
      width: 100% !important;
    }
  }
}
</style>
